package com.le.enhance.class3;

import org.junit.Test;

public class MorrisTest {

    public static class Node {
        int val;
        Node left;
        Node right;
        public Node(int val) {
            this.val = val;
        }
    }

    public void process(Node head) {
        if (head == null) {
            return;
        }
        Node temp = head;
        Node r = null;
        while (temp != null) {
            r = temp.left;
            if (r != null) {
                while (r.right != null && r.right != temp) {
                    r = r.right;
                }
                if (r.right == temp) {
                    r.right = null;
                } else {
                    r.right = temp;
//                    System.out.print(temp.val + "  ");
                    temp = temp.left;
                    continue;
                }
            } else {
//                System.out.print(temp.val + "  ");
            }
            System.out.print(temp.val + "  ");
            temp = temp.right;
        }
    }

    public void pro(Node head){
        if (head == null){
            return;
        }
//        System.out.print(head.val + "  ");
        pro(head.left);
        System.out.print(head.val + "  ");
        pro(head.right);
    }

    @Test
    public void test() {
        Node head = new Node(4);
        head.left = new Node(2);
        head.right = new Node(6);
        head.left.left = new Node(1);
        head.left.right = new Node(3);
        head.right.left = new Node(5);
        head.right.right = new Node(7);
        pro(head);
        System.out.println();
//        morrisIn(head);
        process(head);
    }
}
